//
//  HRShowGuideView.m
//  Hear
//
//  Created by mac on 2017/4/27.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowGuideView.h"

@implementation HRShowGuideView

+(void)showGuideViewImageName:(NSString *)imageName andGuideFrame:(CGRect)guideFrame andSecondeGuideFrame:(CGRect)secondeFrame andSecondeImageName:(NSString *)secondeName
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowGuideView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowGuideView class]]){
        
        
        HRShowGuideView *shareView = (HRShowGuideView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadGuideView:imageName andGuideFrame:guideFrame andSecondeGuideFrame:secondeFrame andSecondeImageName:secondeName];
        
    }
}

- (void)loadGuideView:(NSString *)imageName andGuideFrame:(CGRect)guideFrame andSecondeGuideFrame:(CGRect)secondeFrame andSecondeImageName:(NSString *)secondeimageName{
    UIView *guideView = [[UIView alloc] initWithFrame:self.bounds];
    guideView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self addSubview:guideView];
    
    WeakSelf
    UITapGestureRecognizer *closeTap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf closeView];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:imageName];
    }];
    [guideView addGestureRecognizer:closeTap];
    
    
    
    
    UIImageView *showImageView = [[UIImageView alloc] initWithFrame:guideFrame];
    showImageView.contentMode = UIViewContentModeScaleAspectFill;
    [guideView addSubview:showImageView];
    showImageView.image = [UIImage imageNamed:imageName];
    
    
    UIImageView *showSecondImageView = [[UIImageView alloc] initWithFrame:secondeFrame];
    showSecondImageView.contentMode = UIViewContentModeScaleAspectFill;
    [guideView addSubview:showSecondImageView];
    showSecondImageView.image = [UIImage imageNamed:secondeimageName];
    
    
    
}


-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
